/*
 * bootup.c
 *
 *  Created on: 2011/06/05
 *      Author: AKS
 */

#include "core/include/aks_processor.h"
#include "core/include/process.h"
#include "core/include/types.h"

#include "app/include/test.h"
#include "app/subsystem/include/subSystemManager.h"
#include "app/prompt/include/prompt_tsk.h"
#include "app/tinySystem/include/tinySystem.h"

#include "test/include/auto_test.h"

/* boot up function */
void bootUpFunction(void *ptr);
BOOL bootUpWakeFunction(void *ptr);

/* boot up process */
static PROCESS initialProcess =
{
    bootUpFunction,
    NULL,
    bootUpWakeFunction,
    NULL,
    0
};

LIST_ITEM initialProcessItm =
{
    NULL,
    &initialProcess
};

void bootUpFunction(void *ptr)
{
    UART_transmitString(UART_DBG, (BYTE *)"run boot up process\r\n");

/*
    entryNewProcess(&testItm1);
    entryNewProcess(&testItm2);
    entryNewProcess(&testItm3);
*/
/*
    BYTE ss_info_1[5] = {0x10, 0xAA, 0x01, SYS_TYPE_USB_HOST, 0x50};
    SS_updateDeviceInformation(1, ss_info_1, 5, 0);

    BYTE ss_info_2[5] = {0x10, 0xAA, 0x01, SYS_TYPE_LCD_BUFFER, 0x50};
    SS_updateDeviceInformation(2, ss_info_2, 5, 0);

    BYTE ss_info_4[5] = {0x10, 0xAA, 0x01, SYS_TYPE_CAMERA_BUFFER, 0x50};
    SS_updateDeviceInformation(4, ss_info_4, 5, 0);

    BYTE ss_info_1[5] = {0x10, 0xAA, 0x01, SYS_TYPE_SRAM, 0x50};
    if( TRUE != SS_updateDeviceInformation(4, ss_info_1, 5, 0) )
    {
        UART_transmitString(UART_DBG, (BYTE *)"fail to update subsystem\r\n");
    }
*/


    GIO0_dir_O();
    GIO1_dir_O();

    /* initialize native tasks */
    if(TRUE == init_prompt_tsk(NULL)) entryNewProcess(&promptItm);
    if(TRUE == init_tinySystem_tsk()) entryNewProcess(&tinySystemItm);


    /* initialize sub systems */
    SUB_SYSTEM_reset();

    SS_enumerate();
    SS_showDeviceTable();
    SS_activateTask();

	// test
	#if UNIT_TEST_REQUIRED
	unit_test();
	#endif /*UNIT_TEST_REQUIRED*/

    sleepCurrentProcess(bootUpWakeFunction, NULL);
    UART_transmitString(UART_DBG, (BYTE *)"end boot up process\r\n");	
}

static BOOL singleRunFlag = FALSE;
BOOL bootUpWakeFunction(void *ptr)
{
    if(singleRunFlag)
    {
	    return FALSE;
    }
    else
    {
        singleRunFlag = TRUE;
        return TRUE;
    }
}

